//
//  SplitForceAppDelegate.m
//  SplitForce
//
//  Created by Dave Carroll on 6/20/10.
//  Copyright __MyCompanyName__ 2010. All rights reserved.
//

#import "SplitForceAppDelegate.h"


#import "RootViewController.h"
#import "DetailViewController.h"
#import "LoginViewController.h"


@implementation SplitForceAppDelegate

@synthesize window, splitViewController, rootViewController, detailViewController;
@synthesize loginViewController;


#pragma mark -
#pragma mark Application lifecycle

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {    
    
    // Override point for customization after app launch    
	rootViewController.client = [[ZKSforceClient alloc] init];
	rootViewController.client.useKeyChain = YES;
	
	loginViewController = [[LoginViewController alloc] initWithNibName:@"LoginView" bundle:nil];
	[loginViewController setClient:rootViewController.client];
	
	BOOL showError = NO;
	NSString *errorMsg;
	ZKLoginResult *lr;
	
	if (rootViewController.client.username != @"" && rootViewController.client.password != @"") {
		@try {
			lr = [rootViewController.client login:rootViewController.client.username 
									password:rootViewController.client.password];
		}
		@catch (NSException * e) {
			NSLog(@"%@", [e reason]);
			showError = YES;
			errorMsg = [e reason];
		} @finally {

		}
	}
    
    // Add the split view controller's view to the window and display.
    [window addSubview:splitViewController.view];
    [window makeKeyAndVisible];
    
	if ([rootViewController.client sessionId] == nil) {
		[self showLogin];
	} else {
		[rootViewController didLogin:lr];
	}

	if (showError) {
		[self popupActionSheet:errorMsg];
	}
    return YES;
}

- (void)showLogin {
	loginViewController.modalPresentationStyle = UIModalPresentationFormSheet;
	[splitViewController presentModalViewController:loginViewController animated:YES];
}

- (void)hideLogin {
	[splitViewController dismissModalViewControllerAnimated:YES];
}

-(void)popupActionSheet:(NSString *)message {
    UIActionSheet *popupQuery = [[UIActionSheet alloc]
								 initWithTitle:@"Login Error"
								 delegate:rootViewController
								 cancelButtonTitle:@"OK"
								 destructiveButtonTitle:nil
								 otherButtonTitles:nil];

	[popupQuery setMessage:message];
	popupQuery.actionSheetStyle = UIActionSheetStyleBlackOpaque;
	[popupQuery showInView:loginViewController.view];
	[popupQuery release];
}
- (void)applicationWillTerminate:(UIApplication *)application {
    // Save data if appropriate
}


#pragma mark -
#pragma mark Memory management

- (void)dealloc {
    [splitViewController release];
    [window release];
    [super dealloc];
}


@end

